package com.agricultural.state;

import com.agricultural.equipment.AgriculturalEquipment;

// 工作状态
public class WorkingState implements EquipmentState {
    private AgriculturalEquipment equipment;

    public WorkingState(AgriculturalEquipment equipment) {
        this.equipment = equipment;
    }

    @Override
    public void start(AgriculturalEquipment equipment) {
        System.out.println(equipment.getEquipmentId() + " 已经在工作中");
    }

    @Override
    public void stop(AgriculturalEquipment equipment) {
        System.out.println(equipment.getEquipmentId() + " 已停止工作");
        equipment.setState(new IdleState(equipment));
    }

    @Override
    public void performTask(AgriculturalEquipment equipment) {
        if (equipment.getFuelLevel() < 10) {
            System.out.println(equipment.getEquipmentId() + " 燃油不足，无法继续执行任务");
            equipment.setState(new LowFuelState(equipment));
            return;
        }

        System.out.println(equipment.getEquipmentId() + " 正在执行任务...");
        equipment.performSpecificTask();
    }

    @Override
    public void handleEmergency(AgriculturalEquipment equipment, String emergencyType) {
        System.out.println(equipment.getEquipmentId() + " (工作状态) 收到紧急情况: " + emergencyType);

        if ("obstacle".equalsIgnoreCase(emergencyType)) {
            System.out.println(equipment.getEquipmentId() + " 检测到障碍物，紧急停止");
            equipment.setState(new EmergencyStopState(equipment));
        } else if ("low_fuel".equalsIgnoreCase(emergencyType)) {
            System.out.println(equipment.getEquipmentId() + " 燃油不足，前往加油站");
            equipment.setState(new LowFuelState(equipment));
        }
    }

    @Override
    public String getStateName() {
        return "工作中";
    }
}